package com.yuyou.fn.common.util;

/**
 * Created by Administrator on 2017/4/1.
 */


import com.alibaba.fastjson.JSONObject;

import java.io.IOException;
import java.io.Reader;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;


/**
 * 获取经纬度
 *
 * @author jueyue 返回格式：Map<String,Object> map map.put("status",
 * reader.nextString());//状态 map.put("result", list);//查询结果
 * list<map<String,String>>
 * 密钥:a6y2ygbqRqPOWqlMATKWOkFK
 */
public class MapBaidu {
    private static final  String  ak="a6y2ygbqRqPOWqlMATKWOkFK";
    private static final  String  geocoderUrl="http://api.map.baidu.com/geocoder/v2/";
    private static final  String  locationUrl="http://api.map.baidu.com/location/ip";





    private static String readAll(Reader rd) throws IOException {
        StringBuilder sb = new StringBuilder();
        int cp;
        while ((cp = rd.read()) != -1) {
            sb.append((char) cp);
        }
        return sb.toString();
    }
    public static JSONObject readJsonFromUrl(String ip){
        String json = loadJSON(locationUrl+"?ak="+ak+"&ip="+ip+"&coor=bd09ll");
        JSONObject obj = JSONObject.parseObject(json);
        return  obj;
    }

    public  static  JSONObject findPositionByLngAndLat(Double lng,Double lat){
        String location=lat.toString()+","+lng.toString();
        String url=geocoderUrl+"?ak="+ak+"&%20callback=renderReverse&location="+location+"8&output=json";
        String json = loadJSON(url);


        JSONObject obj = JSONObject.parseObject(json);
        return  obj;
   }


    public static Map<String,Double> findLngAndLat(String address){
        Map<String,Double> map=new HashMap<String, Double>();
        String url = geocoderUrl+"?address="+address+"&output=json&ak="+ak;
        String json = loadJSON(url);
        JSONObject obj = JSONObject.parseObject(json);
        if(obj.get("status").toString().equals("0")){
            double lng=obj.getJSONObject("result").getJSONObject("location").getDouble("lng");
            double lat=obj.getJSONObject("result").getJSONObject("location").getDouble("lat");
            map.put("lng", lng);
            map.put("lat", lat);
            //System.out.println("经度："+lng+"---纬度："+lat);
        }else{
            //System.out.println("未找到相匹配的经纬度！");
        }
        return map;
    }

    public static String loadJSON (String urlString) {

        StringBuilder res = new StringBuilder(100);

        try {
            URL url = new URL(urlString);
            java.net.HttpURLConnection conn = (java.net.HttpURLConnection)url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream(),"UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                res.append(line).append("\n");
            }
            in.close();
        } catch (Exception e) {
            System.out.println("error in wapaction,and e is " + e.getMessage());
        }
        System.out.println(res);
        return res.toString() ;


       /* StringBuilder json = new StringBuilder();
        try {
            URL oracle = new URL(url);
            URLConnection yc = oracle.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    yc.getInputStream()));
            String inputLine = null;
            while ( (inputLine = in.readLine()) != null) {
                json.append(inputLine);
            }
            in.close();
        } catch (MalformedURLException e) {
        } catch (IOException e) {
        }*/
       // return json.toString();
    }


}
